/****************************************Copyright (c)****************************************************
 
**--------------File Info---------------------------------------------------------------------------------
** File name:                key.h
** Last modified Date:       2024.08.07
** Last Version:             Ver:1.0
** Descriptions:             电键处理模块
**                        
**--------------------------------------------------------------------------------------------------------
** Created by:               adm
** Created date:             2024.08.07
** Version:                  Ver:1.0
** Descriptions:             电键处理模块
**
**--------------------------------------------------------------------------------------------------------*/
/*
Copyright (c) [2024]     [BA6IV  ]   [SINEWAVE] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan
PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#ifndef __KEY_H
#define __KEY_H

#ifdef __cplusplus
extern "C" {
#endif

#include "at32f415.h"
#include "at32f415_wk_config.h"
#include "at32f415_board.h"


// 宏定义区
#define Key_Delay           (10)            //电键防抖延时计数器，每单位1ms

#define KEY_L_PIN           GPIO_PINS_0
#define KEY_L_GPIO_PORT     GPIOA
#define Read_KEY_L()        gpio_input_data_bit_read(KEY_L_GPIO_PORT, KEY_L_PIN)

#define KEY_R_PIN           GPIO_PINS_1
#define KEY_R_GPIO_PORT     GPIOA
#define Read_KEY_R()        gpio_input_data_bit_read(KEY_R_GPIO_PORT, KEY_R_PIN)

#define LED_PIN             GPIO_PINS_2
#define LED_GPIO_PORT       GPIOA
#define LED_OFF()           gpio_bits_reset(LED_GPIO_PORT, LED_PIN)        //LED灭 
#define LED_ON()            gpio_bits_set(LED_GPIO_PORT, LED_PIN)          //LED亮

#define Tone_ON()           tmr_counter_enable(TMR1, TRUE)
#define Tone_OFF()          tmr_counter_enable(TMR1, FALSE)


typedef enum {
    straight                = 0x00,
    paddle                  = 0x01
}keymode_t;

typedef enum {
    normal                  = 0x00,
    inversion               = 0x01
} key_change_t;

//电键结构体定义
typedef struct {
    __IO uint8_t            KeyEnable_Flag;         //电键使能标识  0 失能    1 使能
    __IO keymode_t          KEY_Mode;               //0 手键    1 自动键
    __IO uint16_t           speed;                  //自动键速度
    __IO uint16_t           tone;                   //电码音调
    __IO uint8_t            tone_volume;            //电码音调音量
    __IO key_change_t       Autokey_change;         //自动键左右切换  normal:正常  inversion:翻转
    __IO uint8_t            di_flag;                //自动键 嘀标志
    __IO uint8_t            da_flag;                //自动键 嗒标志
    void (*KeyInit)(void);                          //电键初始化函数
    void (*Di)(void);                               //嘀发声函数
    void (*Da)(void);                               //嗒发声函数
} MorseKey_type_t;



/* var extern zone */
extern MorseKey_type_t MyKey;


/* Function zone */
void Di(void);
void Da(void);
void Char_Space(void);
void Word_Space(void);
void Di_num(uint8_t count);
void KeyCheckFun(void);                             //电键状态机服务进程
void KeyProcessorFun(void);                         //电键处理函数

/**
  * @}
  */

#ifdef __cplusplus
}
#endif

#endif
